package org.duiduo;

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

/**
 * spring 项目下测试
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = FDemoApplication.class)
public class SpringDemoTest1 {

    @Autowired
    private ProcessEngine processEngine;
    @Autowired
    private RepositoryService repositoryService;
    @Autowired
    private RuntimeService runtimeService;
    @Autowired
    private TaskService taskService;

    /**
     * 流程部署操作 spring
     */
    @Test
    public void deployTest01() {
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process-01/FirstFlow.bpmn20.xml")
                .name("第一个流程图")
                .deploy();
        System.out.println("deploy id ====> " + deploy.getId());
    }

    /**
     * 流程部署操作 spring
     * 表 => ge_bytearray
     */
    @Test
    public void deployTest02() {
        Deployment deploy = repositoryService.createDeployment()
                .addClasspathResource("process-01/FirstFlow.bpmn20.xml")
                .name("第二个流程图")
                .deploy();
        System.out.println("deploy id ====> " + deploy.getId());
    }

    /**
     * 启动流程实例
     * 表 => ru_task
     */
    @Test
    public void startFlow() {
        // ID是动态维护的 表 => re_procdef
        String processId = "FirstFlow:1:f0c59a3b-482f-11f0-a3e1-2c981178108d";
        String processKey = "FirstFlow"; // 我们自定义的 注意保证唯一
        // 1. 根据流程定义ID启动流程实例
        ProcessInstance processInstance = runtimeService.startProcessInstanceById(processId);
        // 2. 根据流程定义Key启动流程实例
        // runtimeService.startProcessInstanceByKey(processKey);
    }

    /**
     * 待办任务查询
     */
    @Test
    public void findFlow() {
        // 获取到 act_ru_task 中 assignee 字段是 zhangsan 的记录
        List<Task> list = taskService.createTaskQuery()
                // 指定查询的条件
                .taskAssignee("zhangsan")
                .list();
        list.forEach(task -> System.out.println(task.getId()));
    }

    /**
     * 任务审批处理
     */
    @Test
    public void completeTask() {
        // 完成任务的审批 根据任务的ID
        taskService.complete("8329dabe-4835-11f0-b37c-2c981178108d");
    }


}
